<?php


namespace App\Models\Workflow;

use App\Models\Model;
use Illuminate\Support\Facades\Cache;

class Workflow extends Model
{
    protected $table = 'akbus_workflow';

    protected $fillable = [
        'workflow_name',
        'workflow_key',
        'enabled',
    ];

    /**
     * 不可批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [
    ];

    /**
     * 类型转化
     *
     * @var array
     */
    protected $casts = [
    ];

    /**
     * 隐藏的字段
     *
     * @var array
     */
    protected $hidden = [
    ];

    /**
     * 根据工作流名称查找基础信息数据
     *
     * @param $name
     *
     * @return array
     */
    public static function getOptionsArrByName($name): array
    {
        if (! Cache::has('WorkflowAllCache')) {
            Cache::put('WorkflowAllCache', collect(self::with('nodes')->get()->toArray()), 10080);
        }
        $WorkflowAllCache = Cache::get('WorkflowAllCache');

        $category = $WorkflowAllCache->where('workflow_name', $name)->first();

        return $category['nodes'] ?: [];
    }

    /**
     * 关联节点
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function nodes()
    {
        return $this->hasmany('App\Models\Workflow\WorkflowNode', 'workflow_id');
    }
}
